﻿#include "tframework.h"

#include "../../include/ilogger.h"
#include "../../include/iconfig.h"
#include "../../include/inetcomm.h"
#include "../../include/iservice.h"
#include "../../include/icommon.h"
#include "salesman.h"
#include "saleswoman.h"

extern "C" __declspec(dllexport) IFramework* GetInstance()
{
    return dynamic_cast<IFramework*>(tFramework::GetInstance());
}

tFramework::tFramework():
    m_pLogger(NULL),
    m_pConfig(NULL),
    m_pCommon(NULL),
    m_pNetComm(NULL),
    m_pSalesman(NULL),
    m_pSaleswoman(NULL),
    m_pfnCB(NULL),
    m_pUserdata(NULL)
{
}

tFramework::~tFramework()
{

}

int tFramework::Init()
{
    m_pLogger->WriteLog("tFramework::Init()");
    m_mapDllInfo.clear();
    //load Config.dll
    QString strModule = "module/Config.dll";
    QLibrary *pCfgLib = new QLibrary(strModule);
    if (pCfgLib->load()) {
        typedef IConfig* (*GetInstance)();
        GetInstance pGetInstance = (GetInstance)pCfgLib->resolve("GetInstance");
        if (pGetInstance) {
            m_pConfig = pGetInstance();
            m_pConfig->SetFramework(this);
            m_mapDllInfo[dll_Config] = pCfgLib;
            m_pLogger->WriteLog("load Config.dll(1) success!");
        }else{
            m_pLogger->WriteLog("load Config.dll(1) fail!");
        }
    }
    //load NetComm.dll
    strModule = "module/public/NetComm.dll";
    QLibrary *pNetLib = new QLibrary(strModule);
    if (pNetLib->load()){
        typedef INetComm* (*GetInstance)();
        GetInstance pGetInstance = (GetInstance)pNetLib->resolve("GetInstance");
        if (pGetInstance){
            m_pNetComm = pGetInstance();
            m_pNetComm->SetFramework(this);
            m_pNetComm->Init();
            m_mapDllInfo[dll_NetComm] = pNetLib;
            m_pLogger->WriteLog("load NetComm.dll(2) success!");
        }else{
            m_pLogger->WriteLog("load NetComm.dll(2) fail!");
        }
    }
    //load Common.dll
    strModule = "module/Common.dll";
    QLibrary *pCommon = new QLibrary(strModule);
    if (pCommon->load()){
        typedef ICommon* (*GetInstance)();
        GetInstance pGetInstance = (GetInstance)pNetLib->resolve("GetInstance");
        if (pGetInstance){
            m_pCommon = pGetInstance();
            m_pCommon->SetFramework(this);
            m_pCommon->Init();
            m_mapDllInfo[dll_Common] = pCommon;
            m_pLogger->WriteLog("load Common.dll(3) success!");
        }else{
            m_pLogger->WriteLog("load Common.dll(3) fail!");
        }
    }
    /////////////////////////////////
    m_pSalesman = CSalesman::GetInstance();
    m_pSalesman->SetFramework(this);
    m_pSalesman->Init();

    m_pSaleswoman = CSaleswoman::GetInstance();
    m_pSaleswoman->SetFramework(this);
    m_pSaleswoman->Init();

    /////////
    return 1;
}

int tFramework::UnInit()
{
    m_pLogger->WriteLog("tFramework::UnInit()");

    m_pSalesman->UnInit();
    m_pSaleswoman->UnInit();
    m_pConfig->UnInit();
    m_pNetComm->UnInit();
    m_pCommon->UnInit();
    for(int i=dll_Config; i<dll_max; i++){
        QLibrary *pLib = m_mapDllInfo[i];
        if(pLib && pLib->isLoaded()){
            m_szLog = QString("unload dll(%0) success!").arg(i);
            m_pLogger->WriteLog(m_szLog);
            pLib->unload();
            SAFE_DELETE(pLib);
        }
    }
    m_mapDllInfo.clear();
    return 1;
}

int tFramework::SetLogger(ILogger *pLogger)
{
    m_pLogger = pLogger;
    return 1;
}

int tFramework::GetLogger(ILogger **ppLogger)
{
    *ppLogger = m_pLogger;
    return 1;
}

int tFramework::GetCommon(ICommon **ppCommon)
{
    *ppCommon = m_pCommon;
    return 1;
}

int tFramework::GetConfig(IConfig **ppConfig)
{
    *ppConfig = m_pConfig;
    return 1;
}

int tFramework::GetSalesman(IService **ppSaleman)
{
    *ppSaleman = m_pSalesman;
    return 1;
}

int tFramework::GetSaleswoman(IService **ppSalewoman)
{
    *ppSalewoman = m_pSaleswoman;
    return 1;
}

int tFramework::GetNetComm(INetComm **ppNetComm)
{
    *ppNetComm = m_pNetComm;
    return 1;
}

int tFramework::SetCBUICommBridge(pfnUICommBridge pfn, void *pUserData)
{
    if(NULL != pfn)
        m_pfnCB = pfn;
    m_pUserdata = pUserData;
    return 1;
}

int tFramework::GetCBUICommBridge(pfnUICommBridge *ppfn, void **ppUserData)
{
    *ppfn = m_pfnCB;
    *ppUserData = m_pUserdata;
    return 1;
}
